/**
 * Created by LeungZ on 2016/10/17.
 */
// import delTmp from './form/alert.html';
// import delCtrl from './form/alert.ctrl.js';

import addtemp from './form/edit.html';
import addctrl from './form/edit.ctrl.js';

import infoHtml from './configInfo.html';

class Config {
    constructor(ctgNotification, $scope, $uiModal, imageSrv, $stateParams, ctgAlert, ctgColor) {
        "ngInject";
        this._notification = ctgNotification;
        this._uiModal = $uiModal;
        this._imageSrv = imageSrv;
        this._ctgAlert = ctgAlert;
        this._color = ctgColor;
        this.MyimageName = $scope.$root.MyimageName;
        this.imageSrvId = $stateParams.id;
        this.infoHtml = infoHtml;
        this.page = {
            currentPage: 1,
            page: 5,
            totalItems: 0
        };
        this.filter = {
            name: null
        };
        this.getList();
    }

    search() {
        this.table = null;
        this._imageSrv.getsearchList({ configName: this.filter.name, dockerImageTagId: this.imageSrvId, page: this.page.page }).then((data) => {
            data.list.forEach((dat, index) => {
                dat.config = JSON.parse(dat.config);
                dat.border = { 'border-left': '2px groove ' + this._color[index % this._color.length] };
                dat.color = { 'color': this._color[index % this._color.length] };
            });
            this.table = data.list;
            this.page.totalItems = data.total;
        }).catch((re) => {

            if (!this.table) {
                this.table = [];
                this.page.totalItems = 0;
            }
             if (re.resultCode == '1101')return;
            this._notification.error('获取配置文件列表数据有误.');
        })
    }

    getList() {
        this.table = null;
        this.getData();
    }

    getData() {
        this._imageSrv.getConfigList({ page: this.page.page, currentPage: this.page.currentPage, id: this.imageSrvId }).then(data => {

            data.list.forEach((dat, index) => {
                dat.config = JSON.parse(dat.config);
                dat.border = { 'border-left': '2px groove ' + this._color[index % this._color.length] };
                dat.color = { 'color': this._color[index % this._color.length] };
            });
            this.table = data.list;
            this.page.totalItems = data.total;

        }).catch((re) => {

            if (re.resultCode == '1101') {
                if (this.page.currentPage > 1) {
                    this.page.currentPage--;
                    this.getList();
                }else{
                    this.table=[];
                    this.page.totalItems=0;
                }
                
                return;
            }
            if (!this.table) {
                this.table = [];
                this.page.totalItems = 0;
            }
            this._notification.error('获取配置文件列表数据有误.');
        });
    }
    addConfig(item, isVer) {

        this._uiModal.open({
            size: 'lg',
            templateUrl: addtemp,
            controller: addctrl,
            resolve: {
                item: () => { if (isVer) item.isVer = isVer; return item; }
            }
        }).result.then((data) => {
            data.dockerImageTagId = this.imageSrvId;
            if (item && isVer == 2) {
                this._imageSrv.editConfig(data).then(() => {
                    this.getList();
                    this._notification.success('修改配置文件成功');
                }, () => {
                    this._notification.error('修改配置文件失败');
                });
            } else {
                this.adding = true;
                this._imageSrv.addConfig(data).then(() => {
                        this._notification.success('添加配置文件成功');
                        this.getList();
                    }).catch(() => {
                        this._notification.error('添加配置文件失败');
                    })
                    .finally(() => {
                        this.adding = false;
                    });
            }
        })
    }
    deleteConfig(item) {
        this._ctgAlert.open({ title: item.configName + ':' + item.configVersion, action: "删除" }, () => {
            item.deleting = true;
            this._imageSrv.deleteConfig({ id: item.id }).then(() => {
                this._notification.success('删除成功');
                this.getList();
            }).catch(() => {
                this._notification.error('删除失败');
            }).finally(() => {
                item.deleting = false;
            })
        })
    }
    openConfigInfo(el, $event) {

        var $wrap = angular.element($event.currentTarget).parent();
        if (el.open) {
            $wrap.css({ height: 35 });
            el.open = false;
        } else {
            var _height = $wrap.find('table')[0].offsetHeight;
            $wrap.css({ height: _height });
            el.open = true;
        }

    }
}
export default Config